package main;

import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import model.Model;
import view.BlockPanel;
import view.LemonPanel;
import view.UserInputListener;
import controller.Controller;

public class OnPageApplet extends JApplet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2645401938140632360L;
	Model model;
	Controller controller;
	ImageIcon screen;

	public OnPageApplet() {

		model = new Model();
		controller = new Controller(model);
		BlockPanel panelB = new BlockPanel(model);
		LemonPanel panelL = new LemonPanel(model);
		UserInputListener keyListener = new UserInputListener (controller);
		JPanel startScreen = new JPanel();
		JLabel block = new JLabel();

		model.registerWithModel(panelL);
		model.registerWithModel(panelB);

		importSprites();
		block.setIcon(screen);

		startScreen.setSize(800,600);
		startScreen.add(block);
		panelL.add(panelB);
		this.add(startScreen);
		this.add(panelL);

		this.addKeyListener(keyListener);
		this.setFocusable(true);
	}

	public static void main(String[] args) {

		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				JFrame f = new JFrame();
				f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE) ;

				JApplet applet = new OnPageApplet();
				f.add( applet ) ;
				f.setSize(800, 600);
				f.setVisible(true);
			} 
		}
				);
	}

	private ImageIcon importSprite(String path) {
		URL sprite = getClass().getResource(path);
		if (sprite == null) {
			return null;
		} else
			return new ImageIcon(sprite);
	}


	private void importSprites() {
		screen = importSprite("/textures/lemonAdventure.png");
	}
}

